package com.hk.Mgr.web.controller.order;

import com.hk.api.ProcessAPI;
import com.hk.api.exceptions.ValidateException;
import com.hk.base.BaseCtrl;
import com.hk.commons.enm.OrderMonitorEvent;
import com.hk.commons.entity.order.OrderMonitor;
import com.hk.commons.vo.JsonResult;
import com.hk.service.order.IOrderMonitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;

/**
 * Created by Antinomy on 17/2/20.
 */
@RestController
@RequestMapping(value = BaseCtrl.MgrRootPath + "/orderMonitor")
public class OrderMonitorController {

    @Autowired
    private IOrderMonitor orderMonitorRepo;

    private Logger logger = LoggerFactory.getLogger(OrderMonitorController.class);

    @RequestMapping(value = "/preAuthStarted/{orderId}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult preAuthStarted(HttpServletRequest request, @PathVariable Long orderId) {

        ProcessAPI api = (jsonResult, validate) -> {
            validate.check("orderId", orderId);

            orderMonitorRepo.preAuthStarted(orderId);

            jsonResult.result = orderId;
        };

        return api.process(request);
    }

    @RequestMapping(value = "/preAuthFailed/{orderId}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult preAuthFailed(HttpServletRequest request, @PathVariable Long orderId, @RequestBody String result) {

        ProcessAPI api = (jsonResult, validate) -> {
            validate.check("orderId", orderId);

            orderMonitorRepo.preAuthFailed(orderId,result);

            jsonResult.result = orderId;
        };

        return api.process(request);
    }

    @RequestMapping(value = "/preAuthFinish/{orderId}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult preAuthFinish(HttpServletRequest request, @PathVariable Long orderId) {

        ProcessAPI api = (jsonResult, validate) -> {
            validate.check("orderId", orderId);

            orderMonitorRepo.preAuthFinish(orderId);

            jsonResult.result = orderId;
        };


        return api.process(request);
    }

    @RequestMapping(value = "/success/{orderId}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult success(HttpServletRequest request, @PathVariable Long orderId) {

        ProcessAPI api = (jsonResult, validate) -> {
            validate.check("orderId", orderId);

            orderMonitorRepo.success(orderId);

            jsonResult.result = orderId;
        };


        return api.process(request);
    }
}
